//我改了的
var mobile="";

$(function(){   
  
    
 
   $('.date_picker').date_input();
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    $("#startDate").val(y + "-" + m + "-" + d);
      $("#endDate").val("2035-12-30");

    $("#menuContentdevicesingle").css("margin","");
    //时段个数
    for(var i=1;i<=12;i++){
        $("#timing").append("<option>"+i+"</option>");
    }
    

    $("#fs").click(function(){
         var leng=0;
         $("#cz table tbody tr").find("td").find("input").each(function(i){
            if($(this).attr("checked")=="checked"){
                leng++;
            }
        });
        $("#sy table tbody tr").find("td").find("input").each(function(i){
            if($(this).attr("checked")=="checked"){
                leng++;
            }
        });
       if(leng==0){
         //  alert(123);
        setTimeout(function(){
        $("#close123").click();
        swal("", "请选择播放节目", "error");
        },500);
       //
   
       }
    });
 
  
  
    $("#setPlay").click(function(){
           
    
     var ids = $("#table_list_1").jqGrid("getGridParam", "selarrrow");
    //  if(ids.length==0){
    //     swal("OMG!","请选择节目","error");
    //     return;
    // }
     //遍历访问这个集合
     $(ids).each(function (index, id){
          //由id获得对应数据行
     var row = $("#table_list_1").jqGrid('getRowData', id);
     //programidval+=row.programid+","
    var zt=false;
     $("#cz table tbody tr").each(function(){

       var  programid=$(this).find("td").eq(1).html();
     // console.log(programid);
        if(row.programid==programid){
            zt=true;
           return false;
        }
     });
     if(!zt){

           var programlist=new Array();
           programlist.push({
            "programid":row.programid,
             "programname":row.festivalname,
             "coid":sessionStorage.getItem("coid"),
             "programmark":0,
            });
              
        // console.log(JSON.stringify(programlist));            
        $.ajax({
            type: 'POST',
            url: domain.testUrl+"/Threeinjection/programManagement/insertAndroidPlaynodeInfo",
            data:{"programlist":JSON.stringify(programlist),"sessionid":sessionStorage.getItem("sid")},
            success: function (response) {
              
                response=JSON.parse(response);
                if(response.code=="200"){
                   // $("#czdata").hide();
                $("#cz table").append("<tr><td style='text-align: center;'><input  val="+row.programid+"  type='checkbox'></td><td>"+row.programid+"</td><td>"+row.festivalname+"</td></tr>");
                $("#cz table thead tr").find("th:eq(0)").find("input").remove();
                $("#cz table thead tr").find("th:eq(0)").html("<input  onclick='all1(this)'  type='checkbox'>");
        
                
        
                $("#cz table tbody tr").eq($("#cz table tbody tr").length-1).find("td").find("input").click(function(){
                    //获取总数量
                  if($(this).attr("checked")!="checked"){
                     $(this).attr("checked",true);
                  }else{
                    // $(this).removeAttr('checked');
                    $(this).attr("checked",false);
                  }
                    var size=$("#cz table tbody tr").length;
                    var leng=0;
                    $("#cz table tbody tr").find("td").find("input").each(function(thi){
                     if($(this).attr("checked")=="checked"){
                         leng=leng+1;
                     }
                    });
                    if(leng==size){
         
                     $("#cz table thead tr").find("th").find("input").attr("checked",true);
                    }else{
                     $("#cz table thead tr").find("th").find("input").attr("checked",false);
                    }
             });
            }
            }
        });


    
    
    }
   
     });

    });
    //查询
    $("#selectPlay").click(function(){
        var leng=0;
        var id="";
    

var size=$("#cz table tbody tr").length;
$("#cz table tbody tr").find("td").find("input").each(function(i){
  
    if($(this).attr("checked")=="checked"){
        leng++;
        id+=$(this).attr("val")+",";
        $(this).parent().parent().remove();
    }
});
 
var coidval=0;
id = id.substring(0, id.length - 1);

if(leng==size){

    coidval=sessionStorage.getItem("coid");
    id=0;
}



    $.ajax({
        type: 'POST',
        url: domain.testUrl+"/Threeinjection/programManagement/deleteAndroidPlaynodeInfo",
        data:{"coid":coidval,"programId":id,"programmark":0,"sessionid":sessionStorage.getItem("sid")},
        success: function (response) {
            response=JSON.parse(response);
            if(response.code=="200"){
        
    
          if($("#cz table tbody tr").length==0){
            $("#cz table thead tr").find("th:eq(0)").find("input").remove();
            $("#cz table thead tr").find("th:eq(0)").html("<input  onclick='all1(this)'  type='checkbox'>");
        }

        if($("#cz table tbody tr").length==0){

            $("#czdata").show();
          }
        //
        
    }
        }
    });
    //deleteAndroidPlaynodeInfo
    });

    $("#delPlay").click(function(){
        var size=$("#sy table tbody tr").length;
        var leng=0; var id="";
        $("#sy table tbody tr").find("td").find("input").each(function(i){
            if($(this).attr("checked")=="checked"){
                leng++;
                id+=$(this).attr("val")+",";
                $(this).parent().parent().remove();
            }
      });
      if(leng==0){
        swal("OMG!","请选择播放节目","error");
      
      }
      var coidval=0;
      id = id.substring(0, id.length - 1);
if(leng==size){

    coidval=sessionStorage.getItem("coid");
    id=0;
}

   
        $.ajax({
            type: 'POST',
            url: domain.testUrl+"/Threeinjection/programManagement/deleteAndroidPlaynodeInfo",
            data:{"coid":coidval,"programId":id,"programmark":1,"sessionid":sessionStorage.getItem("sid")},
            success: function (response) {
                response=JSON.parse(response);
                if(response.code=="200"){
      
      if($("#sy table tbody tr").length==0){
        
        $("#sy table thead tr").find("th:eq(0)").find("input").remove();
        $("#sy table thead tr").find("th:eq(0)").html("<input  onclick='all2(this)'  type='checkbox'>");
    }
    if($("#sy table tbody tr").length==0){

        $("#sydata").show();
      }
        
}
            }
            });
        //swal("OMG!","请选择节目","error");
    });
    });
$(document).ready(function () {

    swal("提示","“播放列表”模块还在开发中","info");
 //节目列表
$.jgrid.defaults.styleUI = "Bootstrap";
$("#table_list_1").jqGrid({
    data: [],
    datatype: "local",
    height: "100%",
    autowidth: true,
    shrinkToFit: true,
    rowNum: 20,
    rowList: [10, 20, 30],
    //colNames: ["编号", "名称", "手机号码"],
    colModel: [ {
        name: 'programid', index: 'programid', align:"center",width: 22, label: "编号", align: 'center',
    },
    {
        name: 'festivalname', index: 'festivalname',align:"center", width: 30, label: "节目名称", align: 'center',
    },
    
],
    pager: "#pager_list_1",
    viewrecords: true,
    multiselect:true,
    caption: "节目列表",
    hidegrid: false,
    ondblClickRow:function(row){
   // $("#update").click();
   // modify(row);
      // $('#myModal').modal('show')
    },
    onPaging:function(pageBtn){
        console.log(":"+pageBtn);
//	    	var re_records = $jqgrid.getGridParam('records');//获取返回的记录数
        var re_page =  $("#table_list_1").getGridParam('page');//获取返回的当前页
        var re_rowNum=  $("#table_list_1").getGridParam('rowNum');//获取每页数
        var re_total=  $("#table_list_1").getGridParam('lastpage');//获取总页数
        //alert(re_total);
        
        if(pageBtn==="next"){
           // initJqgrid(re_page+1,re_rowNum);
          // if()
           pageval=pageval+1;
           cxlist();
        }
        if(pageBtn==="prev"){
            
            pageval=pageval-1;  cxlist();
        }

        if(pageBtn=="last"){
  
            pageval=re_total;
          
            cxlist();

        }
        if(pageBtn=="first"){
  
            pageval=1;
          
            cxlist();

        }
        
        if(pageBtn=="records"){

            sizeval=$(".ui-pg-selbox").val();
            pageval=1;
            cxlist();
        }
    },

});



$("#table_list_1").setGridHeight($(window).height() * 0.65);
$("#table_list_1").setGridWidth($(window).width() * 0.30);

 $(window).resize(function(){

    $("#table_list_1").setGridHeight($(window).height() * 0.65);
    $("#table_list_1").setGridWidth($(window).width() * 0.30);
});
 });
var sizeval=20;
var pageval=1;
var usernameval=$("#usnameval").val();
var conameval="";
var json="";


//节目列表数据
window.cxlist=function(){

    domain.ajaxUrl({
        method: 'POST',
        url: domain.testUrl+"/Threeinjection/programManagement/queryProgramInfolist",
        data: {size:sizeval,page:pageval,coid:sessionStorage.getItem("coid"),festivalname:"",type:"",devicetype:"",sessionid:sessionStorage.getItem("sid")},
       // datatype:"JSON",
        success: function (response) {

          // console.log(response);
             
            json=response;
            var data=JSON.parse(response);
            var reader = {
                root: function (obj) { return data.rows; },
                page: function (obj) { return pageval; },
                total: function (obj) { return data.taotal; },
                records: function (obj) {

                    return data.records;
                },
               
            };
           
            $("#table_list_1").setGridParam({ data: data.rows, localReader: reader }).trigger('reloadGrid');
 
            var re_records = $("#table_list_1").getGridParam('records');
          
              


            if(re_records==0||re_records==null){
                if($(".norecords").html() == null){
                    $("#table_list_1").parent().append("<div class=\"norecords\">没有符合数据</div>");
                }
                $(".norecords").show();
            }else{

                $(".norecords").hide();
            }

            
              $("#table_list_1").setGridHeight($(window).height() * 0.65);
              $("#table_list_1").setGridWidth($(window).width() * 0.30);
           
            // console.log(response);
          
        }
    });
}

$(function(){
    $("button").eq(0).click(function(){
         sizeval=20;
         pageval=1;
         cxlist();
    });
    cxlist();
    
    $('#myTab a').click(function (e) {
        e.preventDefault()
        // $(this).tab('show')
      })


var table="userinfo";
$("#addtj").click(function(){
    var AZ=["user_name","account","user_pwd","company_id","sex","role_id","enter_state","email","mobile"];//字段
    //var valAZ=["user_name","account","user_pwd","company_id","role_id","enter_state","email","mobile"];//字段
  
    //console.log(findval);
    $("#myModal").find("span").empty();
  //  return;
    //var dataadd={tableName:"userinfo","fieldlist":"user_name','account','user_pwd','company_id','sex','role_id','enter_state','email','mobile'",
  //  "stringvalueA":"'"+$("#user_name").val()+"',"+"'"+$("#user_name").val()+"',"+"'"+hex_md5("123456").toUpperCase()+"',"+"'"+$("#company_id").val()+"',"+"'"+$("#sex").val()+"',"+"'"+$("#jiaose").val()+"',"+"'"+$("#static").val()+"',"+"'"+$("#mail").val()+"',"+"'"+$("#mobile").val()+"'"}
    $("#user_name").blur(); $("#mobile").blur();
   $("#mail").blur(); $("#company_name").blur();
    //{“tableName”:”表名”, “fielded”:”user_id+”，” value”:”修改条件”}
     if($("#myModal").find("span").text().length>0){
           return;
     }
if($("#myModalLabel").text()=="新增"){
  //var dataadd={tableName:"userinfo",fieldnameA:"user_name",fieldnameB:"account",fieldnameC:"user_pwd",fieldnameD:"company_id",fieldnameE:"role_id",fieldnameF:"enter_state",fieldnameG:"email",fieldnameH:"mobile",
 // stringvalueA:$("#user_name").val(),stringvalueB:$("#user_name").val(),stringvalueC:hex_md5("123456").toUpperCase(),stringvalueD:$("#company_id").val(),stringvalueE:$("#sex").val(),stringvalueF:$("#jiaose").val(),stringvalueL:$("#static").val(),stringvalueG:$("#mail").val(),stringvalueH:$("#mobile").val()}
 var val=[];//数据
 val.push($("#user_name").val());   
val.push($("#user_name").val());   
val.push(hex_md5("123456").toUpperCase()); 
val.push($("#company_id").val());  
val.push($("#sex").val());  
val.push($("#jiaose").val());  
val.push($("#static").val()); 
val.push($("#mail").val()); 
val.push($("#mobile").val()); 

var findval=domain.fieldthis(AZ,val,table);
   //var json=
  // {"tableName":"userinfo", "fielded":""，” valueid”:”修改条件”,”fieldnameA”:”需要修改的字段”，”stringvalueA”:”修改值” }
    domain.ajaxUrl({
    method: 'POST',
    url:domain.testUrl+"/Threeinjection/global/insert",
    data:findval,
    success: function (response) {
      
        response=JSON.parse(response);
        if(response.code=="200"){

            $(".close").click();
             pageval=1;
          
            cxlist();
            swal("success!", "保存成功", "success");
        }

    }
    });
}else{
   
    var val2=[];//数据
    val2.push($("#user_name").val());   
    val2.push($("#user_name").val());   
    val2.push(hex_md5("123456").toUpperCase()); 
    val2.push($("#company_id").val());  
    val2.push($("#sex").val());  
   // alert($("#jiaose").val());
    val2.push($("#jiaose").val());  
    val2.push($("#static").val()); 
    val2.push($("#mail").val()); 
    val2.push($("#mobile").val()); 
    var findval=domain.fieldupdatethis(AZ,val2,"user_id",$("#user_id").val(),table);
    domain.ajaxUrl({
    method: 'POST',
    url: domain.testUrl+"/Threeinjection/global/update",
    data:findval,
    success: function (response) {
      
        response=JSON.parse(response);
        if(response.code=="200"){
            pageval=1;
            cxlist();
            $(".close").click();
            swal("success!", "修改成功", "success");

        }

    }
    });

}
});
});
 Date.prototype.Format = function(fmt)   
{ 
//author:wangweizhen
  var o = {   
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日
    "h+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}



$(function(){
   
    var programidval="";
    
    $("#del").click(function(){
        programidval="";
        var ids = $("#table_list_1").jqGrid("getGridParam", "selarrrow");
   
        //遍历访问这个集合
        $(ids).each(function (index, id){
             //由id获得对应数据行
        var row = $("#table_list_1").jqGrid('getRowData', id);
        programidval+=row.programid+","
        //console.log("row.ID:"+row.programid);
        });
        programidval=programidval.substring(0,programidval.length-1);
       // console.log(programid);
       if(programidval==""){

        swal("OMG!", "请选择需要删除的节目", "error");
        return;
       }
       $.ajax({
        type: 'POST',
        url: domain.testUrl+"/Threeinjection/programManagement/deleteProgramInfo",
        data:{"programidList":programidval,sessionid:sessionStorage.getItem("sid")},
        
        success: function (response) {
            response=JSON.parse(response);
            if(response.code=="200"){
                  

                pageval=1;
                cxlist();

                  swal("success!", "删除成功", "success");

            }
             
        }
    });
    });

    $("#sbfs").click(function(){
   
   if($("#device_id").val()==""&&$("#companyid").val()==""){
   
    swal("OMG!", "请选择设备或单位", "error");
     return false;
   
   }else{
   
   var busprogramlistid="";
   var tabprogramlistid="";
   $("#cz table tbody tr").find("td").find("input").each(function(i){
    if($(this).attr("checked")=="checked"){
     
        busprogramlistid+=$(this).attr("val")+"&";
      
    }
});
busprogramlistid=busprogramlistid.substring(0,busprogramlistid.length-1);
   $("#sy table tbody tr").find("td").find("input").each(function(i){
    if($(this).attr("checked")=="checked"){
      
        tabprogramlistid+=$(this).attr("val")+"&";
       
    }
});tabprogramlistid=tabprogramlistid.substring(0,tabprogramlistid.length-1);
   // type = type.substring(0, type.length-1);
   // alert(type);
    //return;
    var device_id=$("#device_id").val();
    var reg = new RegExp(",","g");
    device_id=device_id.replace(reg,"&");
     
    console.log(device_id);
    

    $("body").mLoading();
    $.ajax({
           type: 'POST',
           url: domain.testUrl+"/Threeinjection/programManagement/sendAndroidPlaynodeInfo",
           data:{sessionid:sessionStorage.getItem("sid"),"tabprogramlist":busprogramlistid,"busprogramlist":tabprogramlistid,"devicenumberlist":device_id},
           sessionid:sessionStorage.getItem("sid"),
           success: function (response) {
              
          response=JSON.parse(response);
            if(response.code=="200"){
                  

                 $(".mloading-mask").hide();

                  swal("success!", "发送成功", "success");
                 $(".close").click();
                 sizeval=20;
                 pageval=1;
                 cxlist();
           }else{

            $(".mloading-mask").hide();
            swal("OMG!", "发送失败", "error");
       
        
           }
           },error: function (response, ajaxOptions, thrownError) {

            $(".mloading-mask").hide();
            swal("OMG!", "发送失败", "error");
           }
       });
   }
    });

$("#ad").click(function(){

});

var node="";
for(var i=1;i<=12;i++){
    node="<tr><td><label>开始"+i+"</label></td><td><div id='splaytime"+i+"'></div></td>"
         +"<td><label>结束"+i+"</label></td><td><div id='etime"+i+"'></div></td></tr>";
    $("#times").append(node);
    
     showTimer('splaytime'+i,1);
     showTimer('etime'+i,1);
   
}


    
$("#times>tr").hide();
$("#times >tr:eq(0)").show();
$("#timing").on("click",function(){
    for (var i = 0; i <$(this).val(); i++) {
        // $("#times>tr").hide();
        $("#times >tr:eq("+i+")").show();

    }
});
    


 });


 //时间个数选择
 window.timechange = function () {
    
}


//两个时间的间隔
window.interval=function(faultDate,completeTime){
	var stime = Date.parse(new Date(faultDate));
	var etime = Date.parse(new Date(completeTime));
	var usedTime = etime - stime;  //两个时间戳相差的毫秒数
	var days=Math.floor(usedTime/(24*3600*1000));
	//计算出小时数
	var leave1=usedTime%(24*3600*1000);    //计算天数后剩余的毫秒数
	var hours=Math.floor(leave1/(3600*1000));
	//计算相差分钟数
	var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
	var minutes=Math.floor(leave2/(60*1000));
//	var time = days + "天"+hours+"时"+minutes+"分";
var time = minutes;
// console.log("相差"+minutes)
	return time;

}